package org.example.mouse;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

import java.awt.Point;
import java.util.List;

public class GHubController {

    // 定义与DLL的接口
    public interface GHubLibrary extends Library {
        GHubLibrary INSTANCE = Native.load("./ghub_device.dll", GHubLibrary.class);

        int device_open();
        void mouse_down(int button);
        void mouse_up(int button);
        void key_down(int code);
        void key_up(int code);
        void moveR(int x, int y, boolean abs_move);
    }

    private static boolean gmok = false;

    static {
        try {
            GHubLibrary gm = GHubLibrary.INSTANCE;
            gmok = gm.device_open() == 1;
            if (!gmok) {
                System.out.println("未安装ghub或者lgs驱动!!!");
            } else {
                System.out.println("初始化成功!");
            }
        } catch (UnsatisfiedLinkError e) {
            System.out.println("缺少文件或无法加载库: " + e.getMessage());
        }
    }

    // 按下鼠标按键
    public static void pressMouseButton(int button) {
        if (gmok) {
            GHubLibrary.INSTANCE.mouse_down(button);
        }
    }

    // 松开鼠标按键
    public static void releaseMouseButton(int button) {
        if (gmok) {
            GHubLibrary.INSTANCE.mouse_up(button);
        }
    }

    // 点击鼠标
    public static void clickMouseButton(int button) {
        pressMouseButton(button);
        releaseMouseButton(button);
    }

    // 按下键盘按键
    public static void pressKey(int code) {
        if (gmok) {
            GHubLibrary.INSTANCE.key_down(code);
        }
    }

    // 松开键盘按键
    public static void releaseKey(int code) {
        if (gmok) {
            GHubLibrary.INSTANCE.key_up(code);
        }
    }

    // 点击键盘按键
    public static void clickKey(int code) {
        pressKey(code);
        releaseKey(code);
    }

    // 鼠标移动
    public static void mouseXY(int x, int y, boolean absMove) {
        if (gmok) {
            GHubLibrary.INSTANCE.moveR(x, y, absMove);
        }
    }

    // 重载方法，absMove默认为false
    public static void mouseXY(int x, int y) {
        mouseXY(x, y, false);
    }

    public static void main(String[] args) {
        // 使用示例
        if (gmok) {
//            clickMouseButton(1);  // 假设1是左键
//            clickKey(65);        // 假设65是A键

            while (true) {
                try{
//                    GHubController.mouseXY(100, 100);
                    mouseXY(100, 100);
                    Thread.sleep(5000L);
//                    mouseXY(50000, 500, true); // 绝对移动
                }catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }
    }

    public static Point calculateBoundingBoxCenter(List<Point> points) {
        if (points == null || points.isEmpty()) {
            return null;
        }

        int minX = Integer.MAX_VALUE;
        int minY = Integer.MAX_VALUE;
        int maxX = Integer.MIN_VALUE;
        int maxY = Integer.MIN_VALUE;

        for (Point p : points) {
            minX = Math.min(minX, p.x);
            minY = Math.min(minY, p.y);
            maxX = Math.max(maxX, p.x);
            maxY = Math.max(maxY, p.y);
        }

        return new Point((minX + maxX) / 2, (minY + maxY) / 2);
    }

    public static Point findClosestToCenter(List<Point> points) {
        if (points == null || points.isEmpty()) {
            throw new IllegalArgumentException("点列表不能为空");
        }

        // 1. 计算中心点（质心）
        double sumX = 0, sumY = 0;
        for (Point p : points) {
            sumX += p.getX();
            sumY += p.getY();
        }
        Point center = new Point((int)sumX / points.size(), (int)sumY / points.size());

        // 2. 找出距离中心最近的点
        Point closest = points.get(0);
        double minDistance = distanceSquared(closest, center);

        for (int i = 1; i < points.size(); i++) {
            Point current = points.get(i);
            double currentDistance = distanceSquared(current, center);
            if (currentDistance < minDistance) {
                minDistance = currentDistance;
                closest = current;
            }
        }

        return closest;
    }

    // 使用距离平方比较，避免开方运算提高效率
    private static double distanceSquared(Point a, Point b) {
        double dx = a.getX() - b.getX();
        double dy = a.getY() - b.getY();
        return dx * dx + dy * dy;
    }

    // 获取当前鼠标位置
    public static WinDef.POINT getMousePosition() {
        WinDef.POINT point = new WinDef.POINT();
        User32.INSTANCE.GetCursorPos(point);
        return point;
    }
}